package gcu.module.owner.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import gcu.common.context.SecurityContextHolder;
import gcu.module.owner.dao.OwnerDao;
import gcu.module.owner.daomain.Owner;
import gcu.module.property_money.dao.MoneyDao;
import gcu.module.property_money.daomain.PropertyMoney;
import gcu.module.property_money.service.MoneyService;
import gcu.module.room.dao.RoomDao;
import gcu.module.room.daomain.Room;
import gcu.util.Pager;
import gcu.util.SFormt;
import gcu.util.ExcelHandleUtil;
import gcu.util.UploadStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by haol on 2016/9/23.
 */
@Service
public class OwnerService {

      @Autowired
      private OwnerDao ownerDao;
      @Autowired
      private RoomDao roomDao;
      @Autowired
      private MoneyDao moneyDao;


      /*获取全部角色*/
      public List<Owner> queryOwnerList(Owner owner, Pager pager) {
            return ownerDao.queryOwnerList(owner, pager);
      }

      public void add(Owner owner) {

            owner.setCreateTime(SFormt.NOWDATE_y_M_dhms());
            owner.setIsMoney(0);
            ownerDao.addOwner(owner);
            /*新增时没有id，等插入数据库后根据创建时间ququ*/
            Room room = roomDao.queryRoomByNameAndBuildName(owner.getBuildName(), owner.getRoomNum());
            Owner ownerAdd = ownerDao.queryOwnerByCreateTime(owner.getCreateTime());

            roomDao.setRoomIsIn(room.getId(), 1, ownerAdd.getId());
      }

      public Owner queryOwnerByName(String ownerName) {
            return ownerDao.queryOwnerByName(ownerName);
      }

      public List<Owner> queryOwnerByBuild(String ownerName) {
            return ownerDao.queryOwnerByBuild(ownerName);
      }

      public void update(Owner owner) {
            Owner ownerLater = ownerDao.queryOwnerById(owner.getId());
            /*如果业主更换了房间*/
            if (!((owner.getBuildName().equals(ownerLater.getBuildName())) && (owner.getRoomNum().equals(ownerLater.getRoomNum())))) {
                  Room roomLater = roomDao.queryRoomByNameAndBuildName(ownerLater.getBuildName(), ownerLater.getRoomNum());
                  roomDao.setRoomIsIn(roomLater.getId(), 0, null);
                  Room roomNow = roomDao.queryRoomByNameAndBuildName(owner.getBuildName(), owner.getRoomNum());
                  roomDao.setRoomIsIn(roomNow.getId(), 1, owner.getId());
            }
            ownerDao.updateOwner(owner);
      }

      public void setMoneyInfo(int ownerId, int isMoney) {
            ownerDao.setMoneyInfo(ownerId, isMoney);
      }

      public void delete(Integer id) {
            PropertyMoney money = moneyDao.queryMoneyByOwnerId(id);
            if (money != null ){
                  moneyDao.deleteMoney(money.getId());
            }
            roomDao.setRoomIsIn(id, 0, null);
            ownerDao.deleteOwner(id);
      }
      public void delete(List<String> list) {
            for(int    i=0;    i<list.size();    i++)    {
                  String    str    =    list.get(i);
                  Integer id = Integer.parseInt(str);
                  PropertyMoney money = moneyDao.queryMoneyByOwnerId(id);
                  if (money != null ){
                        moneyDao.deleteMoney(money.getId());
                  }
                  /*将房间设置为未入住*/
                  Owner owner = ownerDao.queryOwnerById(id);
                  Room room = roomDao.queryRoomByNameAndBuildName(owner.getBuildName(),owner.getRoomNum());
                  roomDao.setRoomIsIn(room.getId(), 0, null);
            }
            ownerDao.deleteOwner(list);
      }

      public Owner queryOwnerById(Integer id) {
            return ownerDao.queryOwnerById(id);
      }
      public Owner queryOwnerByBuildAndRoom(String buildName,String roomNum) {
            return ownerDao.queryOwnerByBuildAndRoom(buildName,roomNum);
      }

      @Transactional(readOnly = true)
      public Map<String, Object> get(Integer id) {
            Map<String, Object> map = Maps.newHashMap();
            map.put("user", SecurityContextHolder.getUser());
            if (id == null) {
                  map.put("owner", new Owner());
            } else {
                  Owner owner = ownerDao.queryOwnerById(id);
                  map.put("owner", owner);
            }
            return map;
      }


}
